using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Collections;
using System.Data;
using System.IO;
using MyClever.Lib;

namespace MyClever.DefaultPlugins
{
    public class GetDirectories : MissionPlugin
    {
        [Package(Direction.Input, "Defines the directories you like to search through.")]
        enum Paths
        {
            [Column(typeof(string), "Path where you like to search the expected directories.",
               Validation = "IsPath")]
            Path,
            [Column(typeof(bool), "if true, all subfolders will be ignored.",
             DefaultValue = "false")]
            IgnoreSubDirectories
        }

        [Package(Direction.Input, "Defines the search pattern of the directories you like to search for.")]
        enum DirectorySettings
        {
            [Column(typeof(string), "Pattern you like tor search for. (example: p*)")]
            Pattern
        }

        [Package(Direction.Output, "Defines all found directories with the expected data.")]
        enum Output
        {
            [Column(typeof(string), "The expected result of the directory search.")]
            DirectoryName
        }

        [Plugin(PluginDevStatus.Production, "Returns all given directories from your expected directory path.")]
        public GetDirectories()
        {
        }

        public override void Initialize()
        {
            
            //This is the first Function called by the host...
            //Put anything needed to start with here first
        }

        public override void Dispose()
        {
            //Put any cleanup code in here for when the program is stopped
        }

        /// <summary>
        /// Generate new Source-content.
        /// </summary>
        protected override void Work()
        {
            DataTableReader pathsPackage = this.GetDataReader(typeof(Paths));
            while (pathsPackage.Read())
            {
                this.AddNewLoggingMessage(typeof(Paths), String.Format("search in path: {0}", pathsPackage[Paths.Path.ToString()].ToString()));

                SearchOption searchOption = SearchOption.AllDirectories;
                if (true == Convert.ToBoolean(pathsPackage[Paths.IgnoreSubDirectories.ToString()]))
                {
                    searchOption = SearchOption.TopDirectoryOnly;
                }

                DataTableReader directorySettingPackage = this.GetDataReader(typeof(DirectorySettings));
                while (directorySettingPackage.Read())
                {
                    this.AddNewLoggingMessage(typeof(DirectorySettings), String.Format("use search pattern: {0}", directorySettingPackage[DirectorySettings.Pattern.ToString()].ToString()));

                    foreach (string directoryOn in Directory.GetDirectories(pathsPackage[Paths.Path.ToString()].ToString(), directorySettingPackage[DirectorySettings.Pattern.ToString()].ToString(), searchOption))
                    {
                        DataRow newRow = this.CreateNewRowFromPackage(typeof(Output));
                        newRow[Output.DirectoryName.ToString()] = directoryOn;
                        this.AddNewRowToPackage(typeof(Output), newRow);
                    }
                }
            }
        }
    }
}
